package com.neu.utils.riskReader;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class RiskExcelUtil {

    // 定义实体类映射表格的列
    public static class RiskData {
        /**
         * 风险名称
         */
        @ExcelProperty("riskName")
        private String riskName;
        /**
         * 风险类别
         */
        @ExcelProperty("riskCategory")
        private String riskCategory;
        /**
         * 风险描述
         */
        @ExcelProperty("riskDescription")
        private String riskDescription;
        /**
         * 风险取值
         */
        @ExcelProperty("riskValue")
        private Integer riskValue;


        // Getters and Setters
        public String getRiskName() {
            return riskName;
        }

        public void setRiskName(String riskName) {
            this.riskName = riskName;
        }

        public String getRiskCategory() {
            return riskCategory;
        }

        public void setRiskCategory(String riskCategory) {
            this.riskCategory = riskCategory;
        }

        public String getRiskDescription() {
            return riskDescription;
        }

        public void setRiskDescription(String riskDescription) {
            this.riskDescription = riskDescription;
        }

        public Integer getRiskValue() {
            return riskValue;
        }

        public void setRiskValue(Integer riskValue) {
            this.riskValue = riskValue;
        }
    }

    // 读取表格并返回风险数据列表
    public static List<RiskData> readExcel(String filePath) {
        List<RiskData> riskDataList = new ArrayList<>();
        EasyExcel.read(new File(filePath), RiskData.class, new RiskDataListener(riskDataList)).sheet().doRead();
        return riskDataList;
    }

    //从特定的Url下读取文件
    public static List<RiskData> readExcelFromUrl(String urlStr) {
        List<RiskData> riskDataList = new ArrayList<>();
        try {
            URL url = new URL(urlStr);
            InputStream inputStream = url.openStream();
            EasyExcel.read(inputStream, RiskData.class, new RiskDataListener(riskDataList)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return riskDataList;
    }

    // 根据规则计算风险因子
    public static float calculateRiskFactor(List<RiskData> riskDataList) {
        long total = riskDataList.size();
        long count5 = riskDataList.stream().filter(data -> data.getRiskValue() == 5).count();
        long count4 = riskDataList.stream().filter(data -> data.getRiskValue() == 4).count();
        long count3 = riskDataList.stream().filter(data -> data.getRiskValue() == 3).count();

        if (count5 > total * 0.1 || count4 > total * 0.3) {
            return 1.3f;
        } else if (count3 > total * 0.3) {
            return 1.1f;
        } else {
            return 1.0f;
        }
    }

//    public static void main(String[] args) {
//        // 示例：读取文件并计算风险因子
//        String urlStr = "http://152.136.42.110:9115/swpt/1731909844628_%E9%A3%8E%E9%99%A9%E8%AF%84%E4%BC%B0.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=UpqNh8wlUi48ZZAs4ARB%2F20241118%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241118T060404Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=20c9efe6a0288fdd5ad6dc7a302745d216c35ef2414877c67cc86b738566d433"; // 请替换为实际的 URL
//        List<RiskData> riskDataList = readExcelFromUrl(urlStr);
//        double riskFactor = calculateRiskFactor(riskDataList);
//        System.out.println("计算的风险因子为：" + riskFactor);
//    }
}
